﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using QuanLyRapChieuPhim.GUI;
using QuanLyRapChieuPhim.DTO;
using QuanLyRapChieuPhim.BUS;
using QuanLyRapChieuPhim.DAO;
using System.Diagnostics;

namespace QuanLyRapChieuPhim
{
    public partial class QuanLyRapChieu : Form
    {
        private UserControl usc;

        public static int UserKind = 0;
        public static int UserID = 0;
        public static string UserName = "";
        public static bool IsFinished = false;

        public QuanLyRapChieu()
        {
            InitializeComponent();
        }

        public void QuanLyRapChieu_Load(object sender, EventArgs e)
        {
            try
            {
                //frmSPlash.ShowSplash();
                Location = new Point(50, 50);
                LoadDefault();
            }
            catch
            {
            }
            finally
            {
                this.Activate();
            }

        }

        private void tsbtnThongTinDatVe_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTDatVe";
            pnContain.Controls.Add(usc);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            new TTDatVeBUS().Delete_HetHan();
            new SuatChieuBUS().Delete_HetHan();
        }

        private void cbTinhChinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cbTinhChinh.SelectedIndex;
            if (index != -1)
            {
                if (index == 0)
                {
                    this.BackColor = System.Drawing.ColorTranslator.FromHtml("#EEEEEE");
                }
                else if (index == 1)
                {
                    this.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFee");
                }
                else
                {
                    this.BackColor = System.Drawing.ColorTranslator.FromHtml("#eeFFFF");
                }
            }
        }

        private void tsmiCal_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }

        private void TTNhanVien_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTNhanVien";
            pnContain.Controls.Add(usc);
        }

        private void TTKhachHang_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTKhachHang";
            pnContain.Controls.Add(usc);
        }

        private void tsmiChucVu_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "ChucVu";
            pnContain.Controls.Add(usc);
        }

        private void tsmiBackupAndRestore_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscReports();
            //usc.Tag = "TTKhachHang";
            usc = new ucsBackupAndRestore();
            //usc.Tag = "TTKhachHang";
            pnContain.Controls.Add(usc);
        }

        private void tsmiLoaiKyThuat_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "LoaiKyThuat";
            pnContain.Controls.Add(usc);
        }

        private void tsmiDienVien_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "DienVien";
            pnContain.Controls.Add(usc);
        }

        private void tsmiTheLoaiPhim_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TheLoaiPhim";
            pnContain.Controls.Add(usc);
        }

        private void tsmiTinhTrangPhongChieu_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TinhTrangPhongChieu";
            pnContain.Controls.Add(usc);
        }

        private void tsmiTrangThaiPhim_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TrangThaiPhim";
            pnContain.Controls.Add(usc);
        }

        private void tsmiThamSo_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscCapNhatThamSo();
            pnContain.Controls.Add(usc);
        }

        private void tsmiBackupAndRestore_Click_1(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new ucsBackupAndRestore();
            pnContain.Controls.Add(usc);
        }

        private void tsmiReports_Click(object sender, EventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscReports();
            pnContain.Controls.Add(usc);
        }

        void LoadDefault()
        {
            pnContain.Controls.Clear();
            usc = new uscDanhSachSuatChieu(UserName, UserID, UserKind);

            pnContain.Controls.Add(usc);
            timer1.Start();
            cbTinhChinh.Items.Clear();
            cbTinhChinh.Items.Add("Giao diện mặc định");
            cbTinhChinh.Items.Add("Giao diện 1");
            cbTinhChinh.Items.Add("Giao diện 2");
            cbTinhChinh.SelectedIndex = 0;

            if (UserID == 0)
            {
                lbDangXuat.Visible = false;
                lbTenDangNhap.Text = "";
                lbTenDangNhap.Visible = false;

                rbpgDangNhap.Visible = true;

                rbpHeThong.Visible = false;
                rbpQuanLyHeThong.Visible = false;
                rbpQuanLyNguoiDung.Visible = false;

                bbtnMayTinh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
            else
            {
                lbDangXuat.Visible = true;
                lbTenDangNhap.Text = "Chào!, " + UserName;
                lbTenDangNhap.Visible = true;

                rbpgDangNhap.Visible = false;

                bbtnMayTinh.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                bbtnQLRap.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLPhongChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLCaChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLSuatChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLVe.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLVeBan.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

                bbtnQLChucVu.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLLoaiKyThuat.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLDienVien.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLTheLoaiPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLTrangThaiPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLTinhTrangPhongChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                bbtnQLThamSo.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

                rbpHeThong.Visible = false;
                rbpQuanLyHeThong.Visible = false;
                rbpQuanLyNguoiDung.Visible = false;

                rbpgTaiKhoan.Visible = false;
                rbpgThongTin.Visible = false;
                rbpgTTDatVe.Visible = false;
                rbpgQLTuyChon.Visible = false;

                switch (UserKind)
                {
                    //1	Admin
                    case 1:
                        bbtnQLRap.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLPhongChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLCaChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLSuatChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLVe.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLVeBan.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                        bbtnQLChucVu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLLoaiKyThuat.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLDienVien.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLTheLoaiPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLTrangThaiPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLTinhTrangPhongChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLThamSo.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                        rbpHeThong.Visible = true;
                        rbpQuanLyHeThong.Visible = true;
                        rbpQuanLyNguoiDung.Visible = true;
                        rbpgQLTuyChon.Visible = true;

                        rbpgTaiKhoan.Visible = true;
                        rbpgThongTin.Visible = true;
                        rbpgTTDatVe.Visible = true;
                        break;
                    //2	Trưởng bộ phận
                    case 2:
                        rbpHeThong.Visible = false;

                        bbtnQLPhongChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                        rbpHeThong.Visible = true;
                        rbpQuanLyHeThong.Visible = true;

                        break;
                    //3	Quản lý
                    case 3:
                        bbtnQLCaChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLSuatChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                        rbpHeThong.Visible = true;
                        rbpQuanLyHeThong.Visible = true;

                        break;
                    //4	Nhân viên bán vé
                    case 4:
                        bbtnQLVe.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLVeBan.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                        rbpHeThong.Visible = true;
                        rbpQuanLyHeThong.Visible = true;

                        rbpQuanLyNguoiDung.Visible = true;
                        rbpgTTDatVe.Visible = true;
                        break;
                    //5	Nhân viên rạp
                    case 5:
                        bbtnQLPhongChieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                        bbtnQLPhim.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                        rbpHeThong.Visible = true;
                        rbpQuanLyHeThong.Visible = true;

                        break;
                    case 6:
                        //6	Khách hàng
                        bbtnMayTinh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                        break;
                    default:
                        break;
                }
            }
        }

        private void lbDangXuat_Click(object sender, EventArgs e)
        {
            UserKind = 0;
            UserID = 0;
            UserName = "";
            LoadDefault();
        }
        
        private void bbtnSuatChieu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                pnContain.Controls.Clear();
                usc = new uscDanhSachSuatChieu(UserName, UserID, UserKind);
                pnContain.Controls.Add(usc);
                timer1.Start();
                cbTinhChinh.Items.Clear();
                cbTinhChinh.Items.Add("Giao diện mặc định");
                cbTinhChinh.Items.Add("Giao diện 1");
                cbTinhChinh.Items.Add("Giao diện 2");
                cbTinhChinh.SelectedIndex = 0;
            }
            catch
            {

            }
            finally
            {
                this.Activate();
            }
        }

        private void bbtnDangNhap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new frmDangNhap().ShowDialog();
            if (IsFinished)
            {
                LoadDefault();
            }
        }

        private void bbtnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            timer1.Stop();
            Close();
        }

        private void bbtnBackupAndRestore_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new ucsBackupAndRestore();
            pnContain.Controls.Add(usc);
        }

        private void bbtnReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscReports();
            pnContain.Controls.Add(usc);
        }

        private void bbtnExportKhachHang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (SaveFileDialog1.ShowDialog() == DialogResult.Cancel)
                    return;
                else
                {
                    DataProvider_Excel.FileName = SaveFileDialog1.FileName;
                    DataProvider_Excel dp_e = new DataProvider_Excel();
                    dp_e.Ex_KhachHang();
                    MessageBox.Show("Export thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Export không thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bbtnExportNhanVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (SaveFileDialog1.ShowDialog() == DialogResult.Cancel)
                    return;
                else
                {
                    DataProvider_Excel.FileName = SaveFileDialog1.FileName;
                    DataProvider_Excel dp_e = new DataProvider_Excel();
                    dp_e.Ex_NhanVien();
                    MessageBox.Show("Export thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Export không thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bbtnImportKhachHang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (OpenFileDialog1.ShowDialog() == DialogResult.Cancel)
                    return;
                else
                {
                    DataProvider_Excel.FileName = OpenFileDialog1.FileName;
                    DataProvider_Excel dp_e = new DataProvider_Excel();
                    dp_e.Import_KhachHang();
                    MessageBox.Show("Import thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Import không thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bbtnNhanVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (OpenFileDialog1.ShowDialog() == DialogResult.Cancel)
                    return;
                else
                {
                    DataProvider_Excel.FileName = OpenFileDialog1.FileName;
                    DataProvider_Excel dp_e = new DataProvider_Excel();
                    dp_e.Import_NhanVien();
                    MessageBox.Show("Import thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Import không thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bbtnQLRap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "Rap";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLPhongChieu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "PhongChieu";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLPhim_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "Phim";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLCaChieu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "CaChieu";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLSuatChieu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "SuatChieu";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLVe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "VeXemPhim";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLVeBan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTMuaVe";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQlChucVu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "ChucVu";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLLoaiKyThuat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "LoaiKyThuat";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLDienVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "DienVien";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLTheLoaiPhim_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TheLoaiPhim";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLTrangThaiPhim_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TrangThaiPhim";
            pnContain.Controls.Add(usc);
        }

        private void bbtnQLTrangThaiPhongChieu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TinhTrangPhongChieu";
            pnContain.Controls.Add(usc);
        }

        private void bbtnThamSo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscCapNhatThamSo();
            pnContain.Controls.Add(usc);
        }

        private void bbtnTKKhachHang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "KhachHang";
            pnContain.Controls.Add(usc);
        }

        private void bbtnTKNhanVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "NhanVien";
            pnContain.Controls.Add(usc);
        }

        private void bbtnTTKhachHang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTKhachHang";
            pnContain.Controls.Add(usc);
        }

        private void bbtnTTNhanVien_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTNhanVien";
            pnContain.Controls.Add(usc);
        }

        private void bbtnTTDatVe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscQuanLy();
            usc.Tag = "TTDatVe";
            pnContain.Controls.Add(usc);
        }

        private void bbtnHuongDanSuDung_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Process.Start("Tro giup.chm");
        }

        private void bbtnMayTinh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Process.Start("calc.exe");
        }

        private void bbtnAbout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pnContain.Controls.Clear();
            usc = new uscAbout();
            pnContain.Controls.Add(usc);
        }

    }
}
